package icom.acm.dwr;

import icom.acm.bean.T_USERONLINE;
import icom.acm.manager.T_UserOnlineMgr;
import icom.acm.util.DBConnect;
import icom.acm.util.RedisUtil;
import icom.acm.util.StaticData;
import icom.acm.util.json.JSONMsg;
import icom.acm.util.redis.RedisDB;

import org.directwebremoting.ScriptBuffer;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;

import redis.clients.jedis.Jedis;

public class MessageReplyDWR {
	public MessageReplyDWR(){}
	
	public String getTest(){
		WebContext wctx = WebContextFactory.get();
        String currentPage = wctx.getCurrentPage();
        System.out.println(currentPage);
		System.out.println("Hello, MessageDWR!");
		return "Hello, MessageDWR!";
	}
	
	public void send(String id){
		JSONMsg jmsg = null;
		
		DBConnect db = new DBConnect();
		Jedis jedis = RedisDB.newInstance().get_Jedis();
		T_UserOnlineMgr uomgr= new T_UserOnlineMgr();
		
		T_USERONLINE uo = uomgr.selectBySessionId(db, id);
		String key = RedisUtil.KEY_MESSAGEREPLY2USER_CNT + uo.getUO_U_ID();
		String s_cnt = jedis.get(key);
		jmsg = new JSONMsg(true, s_cnt);
		
		ScriptBuffer script = new ScriptBuffer();
    	script.appendScript("receiveMessageReply(")
        .appendData(jmsg.toString())
        .appendScript(");");
    	
    	try {
    		ScriptSession scriptSession = StaticData.SESSIONID_SCRIPTSESSION_MAP.get(id);
    		scriptSession.addScript(script);
    	} catch (Exception ex) {
    		ex.printStackTrace();
    	}
	}
}
